package com.cwvs.controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cwvs.helper.PingPlusPlus;
import com.cwvs.helper.UrlHelper;
import com.cwvs.services.AppNeedOrderService;
import com.cwvs.utils.AppConstants;
import com.cwvs.utils.CommonUtils;
import com.cwvs.utils.Pages;

@Controller
@RequestMapping("/admin/need")
public class AdminAppNeedOrderController {
	@Resource
	private AppNeedOrderService appNeedOrderService;
	@Resource
	private PingPlusPlus pingPlusPlus;
	@Resource
	private UrlHelper urlHelper;
	private static Logger logger = Logger.getLogger(AdminAppNeedOrderController.class
			.getName());
	
	@RequestMapping("/view")
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_need/app_need_list");
		return mv;
	}
	@RequestMapping("/list")
	public @ResponseBody
	Map<String, Object> list(HttpServletRequest request,
			HttpServletResponse response) {
		Pages page = new Pages();
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		page.setPageSize(Integer.parseInt(limit));
		page.setCurrentPage(Integer.parseInt(start) / Integer.parseInt(limit)
				+ 1);
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("TITLE", request.getParameter("title"));
		paramsMap.put("USER_NAME", request.getParameter("user_name"));
		paramsMap.put("IS_SHOW", "1");
		Pages rePage = appNeedOrderService.needList(paramsMap, page);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", rePage.getPage());
		ret.put("totalCount", rePage.getTotalRecord());
		return ret;
	}
	
	@RequestMapping("/edit")
	public ModelAndView storeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_need/app_need_edit");
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("ID", request.getParameter("id"));
		Map<String, Object> map = appNeedOrderService.getInfoById(m);
		mv.addObject("model", map);
		return mv;
	}
	
	@RequestMapping("/accept_list")
	public @ResponseBody
	Map<String, Object> acceptList(HttpServletRequest request,
			HttpServletResponse response) {
		Pages page = new Pages();
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		page.setPageSize(Integer.parseInt(limit));
		page.setCurrentPage(Integer.parseInt(start) / Integer.parseInt(limit)
				+ 1);
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		String need_id= request.getParameter("need_id");
		paramsMap.put("NEED_ID", StringUtils.isNotBlank(need_id)?need_id:"1");
		Pages rePage = appNeedOrderService.acceptList(paramsMap, page);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", rePage.getPage());
		ret.put("totalCount", rePage.getTotalRecord());
		return ret;
	}
	
	/**
	 * 退款
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/return")
	public @ResponseBody
	Map<String, Object> returnPay(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("ID", id);
		Map<String,Object> data=appNeedOrderService.getChargeById(m);
		if(null==data){
			res.put("errormsg", "需求不存在");
			res.put("success", false);
			return res;
		}
		int status=MapUtils.getIntValue(data, "STATUS");
		if(status==4||status==5){
			Map<String,Object> st=new HashMap<String,Object>();
			st.put("STATUS", AppConstants.STATUS_CANCEL);
			st.put("STATUS_NAME", "揭榜取消");
			st.put("ID", id);
			if(appNeedOrderService.cancelOrDone(st)){
				res.put("message", "退款请求成功");
				res.put("success", true);
				try {
					pingPlusPlus.retrieveCharge(MapUtils.getString(data, "CHARGE_ID"),data);
				} catch (Exception e) {
					appNeedOrderService.cancelOrDone(data);
					res.put("errormsg", "退款请求失败");
					res.put("success", false);
					logger.error("退款请求错误:"+e.getMessage());
					return res;
				}
				Map<String,Object> cancel=new HashMap<String,Object>();
				cancel.put("NEED_ID", id);
				appNeedOrderService.updateAcceptStatus(cancel);
				return res;
			}else{
				res.put("errormsg", "退款请求成功");
				res.put("success", false);
				return res;
			}
		}
		res.put("errormsg", "此状态不允许退款");
		res.put("success", false);
		return res;
	}
	
	/**
	 * 审核
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/approve")
	public @ResponseBody
	Map<String, Object> approve(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("ID", id);
		Map<String,Object> data=appNeedOrderService.getInfoById(m);
		if(null==data){
			res.put("errormsg", "需求不存在");
			res.put("success", false);
			return res;
		}
		int status=MapUtils.getIntValue(data, "STATUS");
		if(status==4){
			data.put("STATUS", AppConstants.STATUS_DONE);
			data.put("STATUS_NAME", "悬赏中");
			data.put("USERNAME", request.getParameter("username"));
			if(appNeedOrderService.approve(data)){
				res.put("message", "审核成功");
				res.put("success", true);
				return res;
			}else{
				res.put("errormsg", "退款请求成功");
				res.put("success", false);
				return res;
			}
		}
		res.put("errormsg", "此状态不允许审核");
		res.put("success", false);
		return res;
	}
	
	
	/**
	 * 取消需求
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/cancel")
	public @ResponseBody
	Map<String, Object> cancel(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("ID", id);
		Map<String,Object> data=appNeedOrderService.getInfoById(m);
		if(null==data){
			res.put("errormsg", "需求不存在");
			res.put("success", false);
			return res;
		}
		int status=MapUtils.getIntValue(data, "STATUS");
		if(status==3){
			data.put("STATUS", 98);
			data.put("STATUS_NAME", "订单取消");
			if(appNeedOrderService.cancelOrDone(data)){
				res.put("message", "取消成功");
				res.put("success", true);
				return res;
			}else{
				res.put("errormsg", "取消失败");
				res.put("success", false);
				return res;
			}
		}
		res.put("errormsg", "此状态不允许取消");
		res.put("success", false);
		return res;
	}
	/**
	 * 完成需求
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/done")
	public @ResponseBody
	Map<String, Object> done(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("ID", id);
		Map<String,Object> data=appNeedOrderService.getInfoById(m);
		if(null==data){
			res.put("errormsg", "需求不存在");
			res.put("success", false);
			return res;
		}
		int status=MapUtils.getIntValue(data, "STATUS");
		if(status==6){
			data.put("STATUS",AppConstants.STATUS_END);
			data.put("STATUS_NAME", "需求完成");
			if(appNeedOrderService.cancelOrDone(data)){
				res.put("message", "操作成功");
				res.put("success", true);
				return res;
			}else{
				res.put("errormsg", "操作失败");
				res.put("success", false);
				return res;
			}
		}
		res.put("errormsg", "此状态不允许完成");
		res.put("success", false);
		return res;
	}
	
	/**
	 * 接单
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/accept")
	public @ResponseBody
	Map<String, Object> accept(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		String oid=request.getParameter("oid");
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("ID", id);
		Map<String,Object> data=appNeedOrderService.getInfoById(m);
		if(null==data){
			res.put("errormsg", "需求不存在");
			res.put("success", false);
			return res;
		}
		int status=MapUtils.getIntValue(data, "STATUS");
		if(status==5){
			Map<String,Object> stat=new HashMap<String,Object>();
			stat.put("NEED_ID", id);
			stat.put("ID", oid);
			Map<String,Object> status1=new HashMap<String,Object>();
			status1.put("ID", id);
			status1.put("STATUS", 6);
			status1.put("STATUS_NAME", "已揭榜");
			appNeedOrderService.cancelOrDone(status1);
			appNeedOrderService.updateAcceptStatus(stat);
			Map<String,Object> accept=appNeedOrderService.getAcceptOrderById(stat);
			if(null!=accept){
				CommonUtils.sendSms(MapUtils.getString(data, "PHONE"), "26123", new String[]{MapUtils.getString(data, "TITLE"),MapUtils.getString(accept, "PHONE")});
			}
			res.put("message", "接单成功");
			res.put("success", true);
			return res;
		}
		res.put("errormsg", "此状态不允许接单");
		res.put("success", false);
		return res;
	}
}
